<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
    <script type="text/javascript" src="${ctxStatic}/ajaxfileupload/ajaxfileupload.js"></script>
	<title>图文内容管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/picture/picArticle/">图文内容列表</a></li>
		<li class="active"><a href="${ctx}/picture/picArticle/form?id=${picArticle.id}">图文内容<shiro:hasPermission name="picture:picArticle:edit">${not empty picArticle.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="picture:picArticle:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="picArticle" action="${ctx}/picture/picArticle/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">标题：</label>
			<div class="controls">
				<form:input path="title" htmlEscape="false" maxlength="255" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">所属栏目：</label>
			<div class="controls">
				<sys:treeselect id="picCategory" name="picCategory.id" value="${picArticle.picCategory.id}" labelName="picCategory.name" labelValue="${picArticle.picCategory.name}"
					title="所属栏目" url="/picture/picCategory/treeData" extId="${picCategory.id}" cssClass="" allowClear="true"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">关键词：</label>
			<div class="controls">
				<form:input path="keywords" htmlEscape="false" maxlength="255" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">描述：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="1000" class="input-xxlarge "/>
			</div>
		</div>
			<div class="control-group">
				<label class="control-label">图文图片：</label>
				<div class="controls">
				<div style="margin: 10px;">
					<input name="articleFileName" id="articleFileName" type="text" readonly="readonly" style="width: 360px; margin-bottom: 0px;" />
					<input name="articleFile" id="articleFile" type="file" style="display: none" />
					<input id="fileSelect" class="btn btn-primary" type="button" value="选择文件" onclick="$('#articleFile').click();" />
					<input id="fileUpload" class="btn" type="button" value="上传" />
				</div>
				<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>名称</th>
								<th>路径</th>
								<!-- <th>文件类型 0普通 1单图 2多图</th> -->
								<th>排序</th>
								<th>描述</th>
								<shiro:hasPermission name="picture:picArticle:edit"><th width="10">&nbsp;</th></shiro:hasPermission>
							</tr>
						</thead>
						<tbody id="picArticleFileList">
							<input name="fastdfs_file_url" id="fastdfs_file_url" type="hidden" value="http://118.178.252.2:8088/" />
						</tbody>
						<%-- <shiro:hasPermission name="picture:picArticle:edit"><tfoot>
							<tr><td colspan="7"><a href="javascript:" onclick="addRow('#picArticleFileList', picArticleFileRowIdx, picArticleFileTpl);picArticleFileRowIdx = picArticleFileRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission> --%>
					</table>
					<script type="text/template" id="picArticleFileTpl">//<!--
						<tr id="picArticleFileList{{idx}}">
							<td class="hide">
								<input id="picArticleFileList{{idx}}_id" name="picArticleFileList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="picArticleFileList{{idx}}_delFlag" name="picArticleFileList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="picArticleFileList{{idx}}_fileName" name="picArticleFileList[{{idx}}].fileName" type="text" value="{{row.fileName}}" maxlength="100" class="input-small" readonly="readonly" style="width:240px;"/>
							</td>
							<td>
								<input id="picArticleFileList{{idx}}_fileUrl" name="picArticleFileList[{{idx}}].fileUrl" type="hidden" value="{{row.fileUrl}}" maxlength="255" class="input-small "/>
								<img src={{row.fastdfs_file_url}}{{row.fileUrl}} style="width:auto;height:80px;">
							</td>
							<td>
								<input id="picArticleFileList{{idx}}_sort" name="picArticleFileList[{{idx}}].sort" type="text" value="{{row.sort}}" maxlength="11" class="input-small " style="width:45px;"/>
							</td>
							<td>
								<textarea id="picArticleFileList{{idx}}_remarks" name="picArticleFileList[{{idx}}].remarks" rows="4" maxlength="1000" class="input-small " style="width:360px;">{{row.remarks}}</textarea>
							</td>
							<shiro:hasPermission name="picture:picArticle:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#picArticleFileList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var picArticleFileRowIdx = 0, picArticleFileTpl = $("#picArticleFileTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(picArticle.picArticleFileList)};
							for (var i=0; i<data.length; i++){
								addRow('#picArticleFileList', picArticleFileRowIdx, picArticleFileTpl, data[i]);
								picArticleFileRowIdx = picArticleFileRowIdx + 1;
							}
							
							$('#articleFile').change(function() {  
								var fileValue = document.getElementById("articleFile").files; 
								$('#articleFileName').val(fileValue[0].name);  
							});  
							
							$("#fileUpload").click(function() {
						      	if($("#articleFileName").val()==""){
									top.$.jBox.tip('请选择上传文件！','warning');
									return;
								}	
						      	loading('正在上传文件，请稍等...');
						        $.ajaxFileUpload({
						          url : '${ctx}/picture/picArticle/uploadArticleFile',//处理图片脚本
						          secureuri : false,
						          data:{},
						          type:"post",
						          fileElementId : 'articleFile',//file控件id
						          dataType : "json",
								  success: function(data){
									if(data.success){
										top.$.jBox.tip('上传文件成功！','info');
										addRow('#picArticleFileList', picArticleFileRowIdx, picArticleFileTpl, data.picArticleFile);
										$('#articleFileName').val("");  
									}else{
										top.$.jBox.tip('上传文件失败！','warning');
									}},
					                error: function (data, status, e)//服务器响应失败处理函数
					                {
					                    top.$.jBox.tip('上传文件失败！','warning');
					                }
						          })
						       });
							
							
						});
					</script>
				</div>
			</div>
		<div class="form-actions">
			<shiro:hasPermission name="picture:picArticle:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>